package edu.scau.mis.web.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.scau.mis.cloth.entity.GsAttrs;
import edu.scau.mis.cloth.entity.GsStorage;
import edu.scau.mis.cloth.service.GsAttrsService;
import edu.scau.mis.cloth.service.GsStorageService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * (GsStorage)表控制层
 *
 * @author makejava
 * @since 2022-05-24 16:33:53
 */
@RestController
@RequestMapping("gsStorage")
public class GsStorageController {
    /**
     * 服务对象
     */
    @Resource
    private GsStorageService gsStorageService;

    @Resource
    private GsAttrsService gsAttrsService;
    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @GetMapping("/page")
    public PageInfo page(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<GsStorage> list = this.gsStorageService.queryAll();
        PageInfo<List> pageInfo = new PageInfo(list);
        return pageInfo;
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<GsStorage> queryById(@PathVariable("id") String id) {
        return ResponseEntity.ok(this.gsStorageService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @return 新增结果 商品入库
     */
    @GetMapping("{clId}/{quantity}/{clSize}")
    public ResponseEntity<GsStorage> add(@PathVariable("clId") Integer clId,@PathVariable("quantity") Integer quantity,@PathVariable("clSize") String clSize) {
        GsStorage gsStorage = new GsStorage();
        gsStorage.setStDatetime(new Date());
        gsStorage.setClSize(clSize);
        gsStorage.setStQuantity(quantity);
        gsStorage.setClId(clId);
        GsAttrs attrs=new GsAttrs();
        attrs.setClId(clId);
        attrs.setClSize(clSize);
        attrs.setInventory(quantity);
        List<GsAttrs> attrslist=gsAttrsService.queryAll(attrs);
        if(attrslist.size()==0){
            gsAttrsService.insert(attrs);
        }else{
            attrs.setInventory(attrslist.get(0).getInventory()+quantity);
            attrs.setId(attrslist.get(0).getId());
            gsAttrsService.update(attrs);
        }
        return ResponseEntity.ok(this.gsStorageService.insert(gsStorage));
    }

    /**
     * 编辑数据
     *
     * @param gsStorage 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<GsStorage> edit(GsStorage gsStorage) {
        return ResponseEntity.ok(this.gsStorageService.update(gsStorage));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(String id) {
        return ResponseEntity.ok(this.gsStorageService.deleteById(id));
    }

}

